/*
 * PGN_Reader : A simple pgn reader for chess games.
 * Copyright (C) 2011 Laurent BERNABE
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gmail.bernabe.laurent.java_swing.pgn_reader.gui;

import java.awt.Image;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.piece.Piece;
import com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.piece.PieceForce;
import com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.piece.Player;

public final class PiecesImages {
	
	private PiecesImages(){
		loadAllPiecesImages();
	}
	
	public static PiecesImages getInstance(){
		return instance;
	}
	
	public Image getPieceImage(Piece wantedPiece){
		return images.get(wantedPiece);
	}
	
	private void loadAllPiecesImages(){
		for(PieceForce currentPieceForce : PieceForce.values()){
			Piece currentWhitePiece = new Piece(currentPieceForce, Player.white);
			Piece currentBlackPiece = new Piece(currentPieceForce, Player.black);
			images.put(currentWhitePiece, loadImageOfAPiece(currentWhitePiece));
			images.put(currentBlackPiece, loadImageOfAPiece(currentBlackPiece));
		}
		dealWithLoadingErrors();
	}

	private Image loadImageOfAPiece(Piece pieceNeedingImage){
		Image imageToReturn;
		if (pieceNeedingImage.owner != Player.none) {
			StringBuilder pieceFileNameBuilder = new StringBuilder(20);
			pieceFileNameBuilder.append(pieceNeedingImage.owner.toString());
			pieceFileNameBuilder.append('_');
			pieceFileNameBuilder.append(pieceNeedingImage.force.toString());
			pieceFileNameBuilder.append(".png");
			String pieceFileName = pieceFileNameBuilder.toString();
			try {
				InputStream imageInputStream = getClass().getResourceAsStream("/"+pieceFileName);
				imageToReturn = ImageIO.read(imageInputStream);
			}
			catch(Exception exception){
				unloadedImages.add(pieceFileName);
				imageToReturn = null;
			}
		}
		else {
			imageToReturn = null;
		}
		return imageToReturn;
	}
	
	private void dealWithLoadingErrors() {
		if (unloadedImages.size() > 0){
			final StringBuilder errorMessage = new StringBuilder();
			errorMessage.append("The following images could not be loaded : ");
			errorMessage.append('\n');
			for (String currentImageName : unloadedImages){
				errorMessage.append(currentImageName);
				errorMessage.append('\n');
			}
			errorMessage.append("Please, check again your download.");
			
			System.err.println(errorMessage);
			
			SwingUtilities.invokeLater(new Runnable() {
				
				@Override
				public void run() {
					JOptionPane.showMessageDialog(null, errorMessage,
							"Image loading error", JOptionPane.ERROR_MESSAGE);
					System.exit(1);
				}
			});
		}
	}
	
	private static PiecesImages instance = new PiecesImages();
	
	private HashMap<Piece, Image> images = new HashMap<Piece, Image>(12);
	private ArrayList<String> unloadedImages = new ArrayList<String>(12);

}
